  // const API_HOST = ''
import md5 from 'js-md5'
import Public from './public'
const API_HOST =  Public.api
var DATA = {
  token: 'sssdsds',
  timestamp: '',
  sign: '',
  bizId: 111,
  data: { },
  pager: {
    startNo: '',
    endNo: ''
  }
}
var tookingAryy = {
  // 地图列表
  mapListFn: ({commit, state}, parm) => {
    var url = API_HOST + '/logistics/order/trackQuery'
    var data = Public.publicLo.parmToken(parm)
    data = JSON.stringify(data)
    state.http.post(url, data, { emulateJSON: true }).then(function (res) {
      //通过 commit addMapList 方法 统一注入sate
      commit('addMapList', res)
    }).catch(function (res) {
      commit('addMapList', null)
    })
  },
  // 录入新的运输数据
  addOderPost: ({commit, state}, parm) => {
    var url = API_HOST + '/logistics/order/insertTransportData'
    parm.locateFrequency = parseInt(parm.locateFrequency)
    var data = Public.publicLo.parmToken(parm)
    data = JSON.stringify(data)
    console.log(data)
    state.http.post(url, data, { emulateJSON: true }).then(function (res) {
      //通过 commit addMapList 方法 统一注入sate
      res.code = 'addOder'
      commit('addOder', res)
    }).catch(function (res) {
      //接口回掉失败，用于组件判断当前回掉失败
      var parm ={
        body:{status:1001}
      }
      commit('addOder',parm)
    })
  },
  // 我的运输列表(运输状态为运输中的数据)
  transportListFn: ({commit, state}, parm) => {
    var url = API_HOST + '/logistics/order/transportList'
    var data = Public.publicLo.parmToken(parm)
    data = JSON.stringify(data)

    state.http.post(url, data, { emulateJSON: true }).then(function (res) {
      // code 用于判断 当前数据状态 0：正常，1：接口成功但数据集合为[ ] 空，-1：接口请求失败
      if(res.body.data.length >= 1){
        res.code = 0
        commit('transportListTo', res)
      }else {
        res.code = 1
        commit('transportListTo', res)
      }
    }).catch(function (res) {
      res.code = -1
      commit('transportListTo', res)
    })
  },
  // 登陆
  loginFn: ({commit, state}, userName) => {
    var url = API_HOST + '/user/login'
    // 收集登陆参数
    var parm = {
      userName: userName.userName,
      passwd: md5(userName.password + 'IS8b9zvn')
    }
    // 获取当前时间戳
    var date = new Date()
    var time = date.getTime()
    // 获取当前token
    DATA.token = 'eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJsb2dpbiIsImlhdCI6MTUxMzA3NDEyMSwic3ViIjoiLTEiLCJleHAiOjE1MTQ1NDUzNTAsIm5iZiI6MTUxMzA3NDEyMX0.Zc88CxsE8n_N5BNj9zMWH9zyENksT1IfdGcaKmjGGkY'
    DATA.timestamp = time
    DATA.data = parm
    // parm = '{"userName":"ivan","passwd":"e833c14c7f490a431519cb0919407a7c"}'
    var dat = JSON.stringify(parm).split('').sort().join('').split('"').join('')
    DATA.sign = md5(DATA.token + DATA.timestamp + dat)
    //console.log(md5('{"token":"eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJsb2dpbiIsImlhdCI6MTUxMzA3NDEyMSwic3ViIjoiLTEiLCJleHAiOjE1MTQ1NDUzNTAsIm5iZiI6MTUxMzA3NDEyMX0.Zc88CxsE8n_N5BNj9zMWH9zyENksT1IfdGcaKmjGGkY","timestamp":1514453207776,"sign":"824d925397958069ea516450eaaf4028","bizId":111,"data":{"userName":"ivan","passwd":"e833c14c7f490a431519cb0919407a7c"},"pager":{"startNo":0,"endNo":0}}'))
    state.http.post(url, JSON.stringify(DATA), { emulateJSON: true }).then(function (res) {
      commit('updateUser', res)
    })
  },
  // 退出登陆
  loginOutFn: ({commit, state}, parm) => {
    var url = API_HOST + '/user/logout'
    parm = {
      userId: parm.userId
    }
    var data = Public.publicLo.parmToken(parm)
    data = JSON.stringify(data)
    state.http.post(url, data, { emulateJSON: true }).then(function (res) {
      if(res.body.status == 0){
        commit('outStatus',  'out')
      }else {
        commit('outStatus',  'outErorr')
      }
    })
  },
  // 条件搜索
  searchList: ({commit, state}, parm) => {
    var url = API_HOST + 'logistics/order/search'
    var data = Public.publicLo.parmToken(parm)
    // parm = JSON.stringify(parm)
    state.http.post(url, data).then(function (res) {
      // code 用于判断 当前数据状态 0：正常，1：接口成功但数据集合为[ ] 空，-1：接口请求失败
      if(res.body.data.length >= 1){
        res.code = 0
        commit('transportListTo', res)
      }else {
        res.code = 1
        commit('transportListTo', res)
      }
    }).catch(function (res) {
      res.code = -1
      commit('transportListTo', res)
    })
  },
  // 历史记录
  history: ({commit, state}, parm) => {
    var url = API_HOST + '/logistics/order/transportHistory'
    var data = Public.publicLo.parmToken(parm)
    data = JSON.stringify(data)
    state.http.post(url, data, { emulateJSON: true }).then(function (res) {
      // code 用于判断 当前数据状态 0：正常，1：接口成功但数据集合为[ ] 空，-1：接口请求失败
      if(res.body.data.length >= 1){
        res.code = 0
        commit('transportListTo', res)
      }else {
        res.code = 1
        commit('transportListTo', res)
      }
    }).catch(function (res) {
      res.code = 0
      res.code = -1
      commit('transportListTo', res)
    })
  },
  // 运输详情
  detail: ({commit, state}, parm) => {
    var url = API_HOST + '/logistics/order/transportDetail'
    var data = Public.publicLo.parmToken(parm)
    data = JSON.stringify(data)
    state.http.post(url, data, { emulateJSON: true }).then(function (res) {
      commit('updateDetail', res)
    }).catch(function (res) {
      commit('updateDetail', null)
    })
  },
  // 查询号码
  checkId: ({commit, state}, parm) => {
    var url = API_HOST + '/logistics/order/checkId'
    var data = Public.publicLo.parmToken(parm)
    data = JSON.stringify(data)
    state.http.post(url, data, { emulateJSON: true }).then(function (res) {
      // code 用于判断 当前数据状态 0：正常，1：接口成功但数据集合为[ ] 空，-1：接口请求失败
      res.time = new Date().getTime()
      if(res.body.data.length >= 1){
        res.code = 0
        commit('transportListTo', res)
      }else {
        res.code = 1
        commit('transportListTo', res)
      }
    }).catch(function (res) {
      res.code = -1
      commit('transportListTo', res)
    })
  },
  // 查询号码
  checkNumber: ({commit, state}, parm) => {
    var url = API_HOST + '/logistics/order/checkNumber'
    var data = Public.publicLo.parmToken(parm)
    data = JSON.stringify(data)
    state.http.post(url, data, { emulateJSON: true }).then(function (res) {
      if(res.body.data.length >= 1){
        res.code = 0
        commit('transportListTo', res)
      }else {
        res.code = 1
        commit('transportListTo', res)
      }
    }).catch(function (res) {
      res.code = -1
      commit('transportListTo', res)
    })
  },
  // 更新运输状态 transport/order/updateTansportStatus
  updateTansportStatus: ({commit, state}, parm) => {
    var url = API_HOST + '/logistics/order/updateTransportStatus'
    var data = Public.publicLo.parmToken(parm)
    data = JSON.stringify(data)

    state.http.post(url, data, { emulateJSON: true }).then(function (res) {
      res.body.code = 'up'
      commit('updateStatusTo', res)
    }).catch(function (res) {
      res.body.code = 'upCatch'
      commit('updateStatusTo', {body: null})
    })
  },
  // 删除记录
  deletefn: ({commit, state}, parm) => {
    var url = API_HOST + '/logistics/order/delete'
    var data = Public.publicLo.parmToken(parm)
    data = JSON.stringify(data)
    state.http.post(url, data, { emulateJSON: true }).then(function (res) {
      res.body.code = 'del'
      commit('updateStatusTo', res)
    })
  },
  // 支持的城市
  getCityList: ({commit, state}, parm) => {
    var url = API_HOST + '/logistics/country/showCountryList'
    var data = Public.publicLo.parmToken(parm)
    data = JSON.stringify(data)
    state.http.post(url, data, { emulateJSON: true }).then(function (res) {
      commit('commitCityList', res)
    }).catch(function (res) {
      commit('commitCityList', null)
    })
  },
  // 跟新提单号
  updateBillon: ({commit, state}, parm) => {
    var url = API_HOST + '/logistics/order/updateTransportOrder'
    var data = Public.publicLo.parmToken(parm,1)
    data = JSON.stringify(data)
    console.log(data)
    state.http.post(url, data, { emulateJSON: true }).then(function (res) {
      res.body.code = 'updateBillon'
      commit('updateStatusTo', res)
    }).catch(function (res) {
      commit('updateStatusTo', {body: null})
    })
  },
  // 设备列表
  getDeviceList: ({commit, state}, parm) => {
    var url = API_HOST + '/company/device/deviceList'
    var data = Public.publicLo.parmToken(parm)
    data = JSON.stringify(data)
console.log(data)
    state.http.post(url, data, { emulateJSON: true }).then(function (res) {
      commit('updateDeviceList', res)
    }).catch(function (res) {
      commit('updateDeviceList', null)
    })
  },
  // 用户列表
  getUserList: ({commit, state}, parm) => {
    var url = API_HOST + '/user/manage/userList'
    var data = Public.publicLo.parmToken(parm)
    data = JSON.stringify(data)
    state.http.post(url, data, { emulateJSON: true }).then(function (res) {
      commit('updateUserList', res)
    }).catch(function (res) {
      commit('updateUserList', null)
    })
  },
  // 删除用户
  delUserList: ({commit, state}, parm) => {
    var url = API_HOST + '/user/manage/deleteUser'
    var data = Public.publicLo.parmToken(parm)
    data = JSON.stringify(data)
    state.http.post(url, data, { emulateJSON: true }).then(function (res) {
      commit('updateStatusTo', res)
    }).catch(function (res) {
      commit('updateUserList', null)
    })
  },
  // 标记设备使用情况
  updateDeviceUsageStatus: ({commit, state}, parm) => {
    var url = API_HOST + '/company/device/updateDeviceUsageStatus'
    var data = Public.publicLo.parmToken(parm)
    data = JSON.stringify(data)
    state.http.post(url, data, { emulateJSON: true }).then(function (res) {
      commit('updateStatusTo', res)
    }).catch(function (res) {
      commit('updateStatusTo', null)
    })
  },
  // 更新提城市列表
  setCityList: ({commit, state}, parm) => {
    var url = API_HOST + '/user/company/setCityList'
    var data = Public.publicLo.parmToken(parm)
    data = JSON.stringify(data)
    state.http.post(url, data, { emulateJSON: true }).then(function (res) {
      commit('updateStatusTo', res)
    }).catch(function (res) {
      commit('updateUserList', null)
    })
  },
  // 添加用户
  addUser: ({commit, state}, form) => {
    var url = API_HOST + '/user/manage/insertUser'
    var parm = form
    parm.password = md5(parm.password + 'IS8b9zvn')
    var data = Public.publicLo.parmToken(parm)
    data = JSON.stringify(data)
    console.log(data);
    state.http.post(url, data, { emulateJSON: true }).then(function (res) {
      commit('updateStatusTo', res)
    }).catch(function (res) {
      commit('updateUserList', null)
    })
  },
  // 重置密码
  resetPassword: ({commit, state}, parm) => {
    var url = API_HOST + '/user/manage/resetPassword'
    parm.password = md5(parm.password + 'IS8b9zvn')
    var data = Public.publicLo.parmToken(parm)
    data = JSON.stringify(data)
    state.http.post(url, data, { emulateJSON: true }).then(function (res) {
      res.body.code = 0
      commit('updateStatusTo', res)
    }).catch(function (res) {
      res.body.code = -1
      commit('updateUserList', null)
    })
  },
  //公司列表
  getCompanyList: ({commit, state}, parm) => {
    var url = API_HOST + '/user/company/companyList'
    var data = Public.publicLo.parmToken(parm)
    data = JSON.stringify(data)
    state.http.post(url, data, { emulateJSON: true }).then(function (res) {
      commit('updateCompanyList', res)
    }).catch(function (res) {
      //commit('updateUserList', null)
    })
  },
  // 添加设备记录
  addDeviceRecord: ({commit, state}, parm) => {
    var url = API_HOST + '/company/device/addDeviceRecord'
    var data = Public.publicLo.parmToken(parm)
    data = JSON.stringify(data)
    state.http.post(url, data, { emulateJSON: true }).then(function (res) {
      commit('updateStatusTo', res)
    }).catch(function (res) {
      //commit('updateUserList', null)
    })
  },
  // 查询设备数据
  showManages: ({commit, state}, parm) => {
    var url = API_HOST + '/company/device/showManages'
    var data = Public.publicLo.parmToken(parm)
    data = JSON.stringify(data)
    state.http.post(url, data, { emulateJSON: true }).then(function (res) {
      commit('updateRecordList', res)
    }).catch(function (res) {
      //commit('updateUserList', null)
    })
  },
  // 标记设备使用情况
  addOper: ({commit, state}, parm) => {
    var url = API_HOST + '/transOrderOper/addOper'
    var data = Public.publicLo.parmToken(parm)
    data = JSON.stringify(data)
    state.http.post(url, data, { emulateJSON: true }).then(function (res) {
      commit('updateStatusTo', res)
    }).catch(function (res) {
      commit('updateStatusTo', null)
    })
  },
  // 修改密码 /user/manage/resetPassword
  resetPassword: ({commit, state}, parm) => {
    var url = API_HOST + '/user/manage/resetPassword'
    parm.oldPasswd = md5(parm.oldPasswd + 'IS8b9zvn')
    parm.newPasswd = md5(parm.newPasswd + 'IS8b9zvn')
    var data = Public.publicLo.parmToken(parm)
    data = JSON.stringify(data)
    state.http.post(url, data, { emulateJSON: true }).then(function (res) {
      res.code = 'up'
      commit('updateStatusTo', res)
    }).catch(function (res) {
      res.code = 'updateStatus'
      commit('updateStatusTo', null)
    })
  },
  tokenWx:({commit, state}, parm) => {
    var url = API_HOST + '/logistics/weixin/singature'
    var data = Public.publicLo.parmToken(parm)
    data = JSON.stringify(data)
    state.http.post(url, data, { emulateJSON: true }).then(function (res) {
      res.code = 'tokenWxFn'
      commit('tokenWxFn', res)
    }).catch(function (res) {
      res.code = 'tokenWxFn'
      commit('tokenWxFn', res)
    })
  },
  //数据分析
    showAnalysisData:({commit, state}, parm) => {
    var url = API_HOST + '/logistics/statistics/showAnalysisData'
    var data = Public.publicLo.parmToken(parm)
      data = JSON.stringify(data)
    state.http.post(url, data, { emulateJSON: true }).then(function (res) {
      res.code = 'showAnalysisData'
      res.time = new Date()
      commit('comShowAnalysisData', res)
    }).catch(function (res) {
      res.code = 'tokenWxFn'
      commit('comShowAnalysisData', res)
    })
  },
  // 修改用户等级
  updateLevel:({commit, state}, parm) => {
    var url = API_HOST + '/user/manage/updateLevel'
    var data = Public.publicLo.parmToken(parm)
    data = JSON.stringify(data)
    state.http.post(url, data, { emulateJSON: true }).then(function (res) {
      res.body.code = 'updateLevel'
      commit('updateStatusTo', res)
    }).catch(function (res) {
      res.body.code = 'updateLevel'
      commit('updateStatusTo', res)
    })
  },
  // 查询公司设备
    findDeviceList:({commit, state}, parm) => {
      var url = API_HOST + '/company/device/findDeviceList'
      var data = Public.publicLo.parmToken(parm)
      data = JSON.stringify(data)
      state.http.post(url, data, { emulateJSON: true }).then(function (res) {
        commit('updateDeviceList', res)
      }).catch(function (res) {
        commit('updateDeviceList', null)
      })
    },
  // 查询全部订单
  stsAllOrderNum:({commit, state}, parm) => {
    var url = API_HOST + '/logistics/statistics/stsAllOrderNum'
    var data = Public.publicLo.parmToken(parm)
    data = JSON.stringify(data)
    state.http.post(url, data, { emulateJSON: true }).then(function (res) {
      commit('commitAllOrderNum', res)
    }).catch(function (res) {
      commit('commitAllOrderNum', 0)
    })
  },
  // 查询月份订单
  stsOrderNumByMonth:({commit, state}, parm) => {
    var url = API_HOST + '/logistics/statistics/stsOrderNumByMonth'
    var data = Public.publicLo.parmToken(parm)
    data = JSON.stringify(data)
    state.http.post(url, data, { emulateJSON: true }).then(function (res) {
      commit('commitOrderNumByMonth', res)
    }).catch(function (res) {
      commit('commitOrderNumByMonth', 0)
    })
  },
  // 查询设备 接口
  deviceShowList:({commit, state}, parm) => {
    var url = API_HOST + '/user/device/showList'
    var data = Public.publicLo.parmToken(parm)
    data = JSON.stringify(data)
    console.log(data)
    state.http.post(url, data, { emulateJSON: true }).then(function (res) {
      res.code = 'DeviceShowList'
       commit('commitDeviceShowList', res)
    }).catch(function (res) {
      res.code = 'DeviceShowListErr0r'
       commit('commitDeviceShowList', res)
    })
  },
  // 添加设备 接口
  addDevice:({commit, state}, parm) => {
    var url = API_HOST + '/user/device/batchAddDevice'
    var data = Public.publicLo.parmToken(parm)
    data = JSON.stringify(data)
    console.log( data)
    state.http.post(url, data, { emulateJSON: true }).then(function (res) {
      res.body.code = 'addDevice'
      commit('updateStatusTo', res)
    }).catch(function (res) {
      res.body.code = 'addDevice'
      commit('updateStatusTo', res)
    })
  },
  // 添加设备 接口
  deleteDevice:({commit, state}, parm) => {
    var url = API_HOST + '/user/device/deleteDevice'
    var data = Public.publicLo.parmToken(parm)
    data = JSON.stringify(data)
    console.log(data)
    state.http.post(url, data, { emulateJSON: true }).then(function (res) {
      res.body.code = 'deleteDevice'
      commit('updateStatusTo', res)
    }).catch(function (res) {
      res.body.code = 'deleteDevice'
      commit('updateStatusTo', res)
    })
  },
 //
   sendDictate:({commit, state}, parm) => {
    var url = API_HOST + '/company/device/sendDictate'
    var data = Public.publicLo.parmToken(parm)
    data = JSON.stringify(data)
    console.log(data)
    state.http.post(url, data, { emulateJSON: true }).then(function (res) {
      res.body.code = 'sendDictate'
      commit('updateStatusTo', res)
    }).catch(function (res) {
      res.body.code = 'sendDictate'
      commit('updateStatusTo', res)
    })
  },
  // 提单号搜索列表
  sechByBlill:({commit, state}, parm) => {
    var url = API_HOST + '/logistics/order/batchCheck'
    var data = Public.publicLo.parmToken(parm)
    data = JSON.stringify(data)
    console.log(data)
    state.http.post(url, data, { emulateJSON: true }).then(function (res) {
      res.body.code = 'sendDictate'
      commit('commitSechByBlill', res)
    }).catch(function (res) {
      res.body.code = 'sendDictate'
      commit('commitSechByBlill', res)
    })
  },
  //未登录提单号查询
  getResource:({commit, state}, parm) => {
    var url = API_HOST + '/user/company/getResource'
    var data = Public.publicLo.parmToken(parm)
    data = JSON.stringify(data)
    console.log(data)
    state.http.post(url, data, { emulateJSON: true }).then(function (res) {
      res.body.code = 'getResource'
      commit('commitGetResource', res)
    }).catch(function (res) {
      res.body.code = 'getResource'
      commit('commitGetResource', res)
    })
  },
  //公司统计分析
  statisticsCompanyDevice:({commit, state}, parm) => {
    var url = API_HOST + '/user/manage/statisticsCompanyDevice'
    var data = Public.publicLo.parmToken(parm)
    data = JSON.stringify(data)
    console.log(data)
    state.http.post(url, data, { emulateJSON: true }).then(function (res) {
      res.body.code = 'statisticsCompanyDevice'
      commit('commitStatisticsCompanyDevice', res)
    }).catch(function (res) {
      res.body.code = 'statisticsCompanyDevice'
      commit('commitStatisticsCompanyDevice', res)
    })
  },
  //公司统计分析设备订单列表
  statisticsTransOrder:({commit, state}, parm) => {
    var url = API_HOST + '/user/manage/statisticsTransOrder'
    var data = Public.publicLo.parmToken(parm)
    data = JSON.stringify(data)
    console.log(data)
    state.http.post(url, data, { emulateJSON: true }).then(function (res) {
      res.body.code = 'statisticsTransOrder'
      commit('commitStatisticsTransOrder', res)
    }).catch(function (res) {
      res.body.code = 'statisticsCompanyDevice'
      commit('commitStatisticsTransOrder', res)
    })
  },
  getModel:({commit, state}, parm)=>{
    var url = API_HOST + '/company/device/getModel'
    var data = Public.publicLo.parmToken(parm)
    data = JSON.stringify(data)
    console.log(data)
    state.http.post(url, data, { emulateJSON: true }).then(function (res) {
      res.body.code = 'getModel'
      commit('commitGetModel', res)
    }).catch(function (res) {
      res.body.code = 'getModel'
      commit('commitGetModel', res)
    })
  },
  lastLocation:({commit, state}, parm)=>{
    var url = API_HOST + '/company/device/lastLocation'
    var data = Public.publicLo.parmToken(parm)
    data = JSON.stringify(data)
    console.log(data)
    state.http.post(url, data, { emulateJSON: true }).then(function (res) {
      res.body.code = 'lastLocation';
      res.body.time = new Date().getTime()
      commit('commitDavislistByMap', res)
    }).catch(function (res) {
      res.body.code = 'lastLocation';
      res.body.time = new Date().getTime()
     commit('commitDavislistByMap', res)
    })
  }

}
export default {tookingAryy}

